<?php

namespace App\Kuafu\Mqtt\Model;

use App\Kuafu\Mqtt\Enum\MqttCodeEnum;
use Illuminate\Support\Facades\Redis;

class Queue
{
    const LOG_QUEUE_NS = '_logQueue'; //日志队列命名空间

    public static function makeQueueKey($shopId): string
    {
        return $shopId . self::LOG_QUEUE_NS;
    }

    public static function add($shopId, $data)
    {
        if ($data['cmd'] == MqttCodeEnum::MQTT_COMMAND_UPDATE_PRODUCT_MENU) {
            return;
        }

        $data = json_encode($data);
        return Redis::rpush(self::makeQueueKey($shopId), $data);
    }

    public static function pop($shopId)
    {
        $data = Redis::lpop(self::makeQueueKey($shopId));
        if ($data) {
            $data = json_decode($data, true);
        }

        return $data;
    }

    /**
     * 获取队列长度
     *
     * @param $shopId
     * @return int
     */
    public static function len($shopId)
    {
        return Redis::llen(self::makeQueueKey($shopId));
    }

}
